package com.neuedu.controller;

import com.neuedu.entity.Category;
import com.neuedu.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: MARK
 * @Date: 2019/8/5 19:26
 * @Version: 1.0.0
 * @Description:
 */
@RestController
@Slf4j
@RequestMapping(value = "/category")
@Transactional
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * @return java.util.List<com.neuedu.entity.Category>
     * @create: 2019/8/5
     * @author MARK
     * @Description: 查询所有
     */
    @GetMapping
    public List<Category> getAll(){
        log.info(categoryService.selectAll().toString());
        return categoryService.selectAll();
    }

    /**
     * @return com.neuedu.entity.Category
     * @create: 2019/8/5
     * @author MARK
     * @Description: 根据id查询
     */
    @GetMapping(value = "{id}")
    public Category getOne(@PathVariable("id") Integer id){
        Category category = categoryService.findById(id);
        log.info(category.toString());
        return category;

    }

    /**
     * @return java.lang.String
     * @create: 2019/8/5
     * @author MARK
     * @Description: 插入数据
     */
    @PostMapping
    public String insert(@RequestBody Category category){
        categoryService.add(category);
        return "ok";
    }

    /**
     * @return java.lang.String
     * @create: 2019/8/5
     * @author MARK
     * @Description: 根据id删除，级联删除
     */
    @DeleteMapping(value = "{id}")
    public String deleteById(@PathVariable("id") Integer id){
        categoryService.deleteById(id);
        return "ok";
    }

    /**
     * @return java.lang.String
     * @create: 2019/8/5
     * @author MARK
     * @Description: 修改信息, 注意只能改name,descr
     */
    @PutMapping
    public String update(@RequestBody Category category){
        categoryService.update(category);
        return "ok";
    }
}
